#!/bin/bash
#
# ------------------------------------------------------------------
#	     Pour Debian ZinuD par MerMouY
#	mermouy <AT> gmail <DOT> com
#	Librement inspiré/adapté des travaux de 
#	sputnick in da FreAkY lApPy lAb (c) 2009
#       gilles.quenot <AT> gmail <DOT> com
#	Idea from smecher.
#
#	Utilisant eux-même des scripts mis à disposition sur 
#	http://stackp.online.fr
# ------------------------------------------------------------------
# ------------------------------------------------------------------
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#       (see http://www.gnu.org/licenses/gpl.html).
# ------------------------------------------------------------------
#
#  Questions et infos : http://zinud.youm.org/forums.html
# 
#  Depends:
#    python
#    zenity
# 

###LANGUE
#Dossier contenant les fichiers de langues
Zinlangrep="/usr/share/zinud/lang"

#Liste des fichiers de langues disponibles listé pour zenity-list
LIST=`ls -m $Zinlangrep | sed -e 's/.cfg,/ FALSE/g' |  sed -e 's/.cfg//g' | sed -e 's/^/FALSE /' | sed -e 's/FALSE en/TRUE en/g'`

#icône des fenêtres
Zinicon="/usr/share/pixmaps/zinud/horned-logo.png"

#Création du dossier de configuration zinud utilisateur s'il n'existe pas

if [ ! -d ~/.config/zinud ]
then
	mkdir -p ~/.config/zinud
fi

#Vérification langue choisie sinon la choisir, ou utiliser eng par défaut

while [ ! -f ~/.config/zinud/lang.cfg ]
do
	. $Zinlangrep/en.cfg
	zenity --question --window-icon="$Zinicon" --title="$Zininstall" --text="$Zininstalltxt" || exit 0
	if [ $? = "0" ]
	then
	Chlang=$(zenity --list \
		--radiolist \
		--title="$Language" \
		--window-icon="$Zinicon" \
		--text="$Langconf" \
		--print-column="2" \
		--column "$Select" \
		--column "$Availang" \
		$LIST) || exit 0
		cp -vf $Zinlangrep/$Chlang.cfg ~/.config/zinud/lang.cfg
		. ~/.config/zinud/lang.cfg
		zenity --info --text="$Langdef"
	else
	cp -vf $Zinlangrep/en.cfg ~/.config/zinud/lang.cfg
	. ~/.config/zinud/lang.cfg
	zenity --info --text="$Engdef"
	fi
done


#on source ce fichier de langue 

. ~/.config/zinud/lang.cfg

###/LANGUE

###VARIABLES

DIR=$1
Extadr="\n\nhttp://$(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d ' ' -f 2 | cut -d\< -f 1):"
Intadr="\n\nhttp://$(/sbin/ifconfig $Iface | awk '/inet/{gsub(" *inet add?r:","");print $1;exit}'):"

# User entry verification (not empty or ask again)

while [ -z $DIR ] || [ ! -d $DIR ]
do
DIR=$(zenity --entry --title="$Dirchoice" --text="$Dirchoicetxt" --entry-text=`pwd`) || exit 0
done

# Is there more than one interface? 

Ethlistnb=$(/sbin/ifconfig | grep Link | cut -d" " -f1 | sed -e '/lo/d'| grep -c ".")

function Ethlistzen {
/sbin/ifconfig | grep Link | cut -d" " -f1 | sed -e '/lo/d' > /tmp/.ethlist
sed -e 's/^/. /' /tmp/.ethlist > /tmp/.ethlistzen
}
Ethlist=$(cat /tmp/.ethlistzen)

#Choose interface

function Choixeth (){
Iface=$(zenity --list \
--radiolist \
--title="$Eth" \
--text="$Ethtxt" \
--print-column="2" \
--column="$Select" \
--column="$Interfaces" \
$Ethlist
) || exit 0
}

#No interface

if [ $Ethlistnb = "0" ]
then
	zenity --error --text="$Ethnoavail"
	exit 0
#More than one interface so zenity list them
elif [ $Ethlistnb != "1" ]
then
	Ethlistzen
	Choixeth
	while [ -z $Iface ]
	do
		zenity --error --text="$Ethnochoice"
		Choixeth
	done
else
#If only one interface
	Iface=`/sbin/ifconfig | grep Link | cut -d" " -f1 | sed -e '/lo/d'`
fi

# Port to listen on

PORT=$(zenity --entry --title="$Port" --text="$Porttxt" --entry-text="8000") || exit 0

# Is port already in use?

Servpid=$(lsof -t -i :$PORT)
if [ ! -z $Servpid ]; then
    zenity --error --text="$Portaken $Servpid\n\n$Portaken1 <b>kill $Servpid</b>\n\n$Portaken2"
    exec $0
fi

if [ -z $PORT ] || [ $PORT = "0" ] || [ $PORT -gt 65535 ]
then
	zenity --info --title="Port" --text="$Portnochoice"
	exec $0
fi

# Last confirmation and summary

zenity --question --title="$Lastask" --text="$Dircontent \"$DIR\" $Rest<b>$Extadr$PORT</b> $On <b>$Iface</b>\n\n$Rest1<b>$Intadr$PORT</b> $On <b>$Iface</b>\n\n$Lastask3" || exit 0

# Does the log file already exist? If so rename it

if [ -f ~/.ShareIt.log ]
then
DATE=$(sed -n '1p' ~/.ShareIt.log | cut -d '[' -f2 | cut -d ']' -f1 | sed -e "s/\//./g" | sed -e "s/:/./g" | sed -e "s/ /./g")
mv ~/.ShareIt.log ~/.ShareIt.$DATE.log
zenity --info --text="$Oldlog $DIR/.ShareIt.$DATE.log"
fi

# Let's start the server & create the logfile

python /usr/local/bin/webserver.py $PORT 2>&1 | tee ~/.ShareIt.log &

# Create notification area to stop the server

while true; do
    zenity --notification  --text="$Hereclick"

    if zenity --question --title="$Confirm" --text="$Confirmtxt<b>$Extadr$PORT</b> $On <b>$Iface\n$Intadr$PORT</b> $On <b>$Iface</b>\n\n$Confirmtxt1"; then
        while [ ! -z `lsof -t -i :$PORT` ]
	do
        	kill `lsof -t -i :$PORT`
	done
        exit $?
    fi
done
